package cn.iocoder.yudao.module.promotion.controller.admin.yearcardconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.promotion.controller.admin.yearcardconfig.vo.YearCardConfigPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.yearcardconfig.vo.YearCardConfigRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.yearcardconfig.vo.YearCardConfigSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.yearcardconfig.YearCardConfigDO;
import cn.iocoder.yudao.module.promotion.service.yearcardconfig.YearCardConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 年卡配置")
@RestController
@RequestMapping("/promotion/year-card-config")
@Validated
public class YearCardConfigController {

    @Resource
    private YearCardConfigService yearCardConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建年卡配置")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:create')")
    public CommonResult<Long> createYearCardConfig(@Valid @RequestBody YearCardConfigSaveReqVO createReqVO) {
        return success(yearCardConfigService.createYearCardConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新年卡配置")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:update')")
    public CommonResult<Boolean> updateYearCardConfig(@Valid @RequestBody YearCardConfigSaveReqVO updateReqVO) {
        yearCardConfigService.updateYearCardConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除年卡配置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:delete')")
    public CommonResult<Boolean> deleteYearCardConfig(@RequestParam("id") Long id) {
        yearCardConfigService.deleteYearCardConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得年卡配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:query')")
    public CommonResult<YearCardConfigRespVO> getYearCardConfig(@RequestParam("id") Long id) {
        YearCardConfigDO yearCardConfig = yearCardConfigService.getYearCardConfig(id);
        return success(BeanUtils.toBean(yearCardConfig, YearCardConfigRespVO.class));
    }

    @GetMapping("/getOne")
    @Operation(summary = "获得年卡配置")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:query')")
    public CommonResult<YearCardConfigRespVO> getYearCardConfig() {
        YearCardConfigDO yearCardConfig = yearCardConfigService.getYearCardConfig();
        return success(BeanUtils.toBean(yearCardConfig, YearCardConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得年卡配置分页")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:query')")
    public CommonResult<PageResult<YearCardConfigRespVO>> getYearCardConfigPage(@Valid YearCardConfigPageReqVO pageReqVO) {
        PageResult<YearCardConfigDO> pageResult = yearCardConfigService.getYearCardConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, YearCardConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出年卡配置 Excel")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-config:export')")
    @OperateLog(type = EXPORT)
    public void exportYearCardConfigExcel(@Valid YearCardConfigPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<YearCardConfigDO> list = yearCardConfigService.getYearCardConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "年卡配置.xls", "数据", YearCardConfigRespVO.class,
                BeanUtils.toBean(list, YearCardConfigRespVO.class));
    }

}